package com.doublefish.doublefishrobot.ui.setting.widget;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;


/**
 * 作者:dugaofeng on 2024/10/16 09:27
 */
public class HandSwitchView extends FrameLayout {

    private Context mContext;

    private View slider;
    private TextView leftTV;
    private TextView rightTV;

    private OnHandSwitchViewListner onHandSwitchViewListner;
    private boolean didGetLayoutParams = false;
    private int index = 0;
    private int offset = 0;

    public HandSwitchView(@NonNull Context context) {
        this(context, null);
    }

    public HandSwitchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HandSwitchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.view_hand_switch, this);
        slider = findViewById(R.id.slider);
        leftTV = findViewById(R.id.tv_left);
        rightTV = findViewById(R.id.tv_right);

        leftTV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectIndex(0, true);
                if (onHandSwitchViewListner != null) {
                    onHandSwitchViewListner.onHandSwitchViewChanged(0);
                }
            }
        });

        rightTV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectIndex(1, true);
                if (onHandSwitchViewListner != null) {
                    onHandSwitchViewListner.onHandSwitchViewChanged(1);
                }
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (!didGetLayoutParams) {
            didGetLayoutParams = true;

            int width = getLayoutParams().width;
            offset = width / 2;

            ViewGroup.LayoutParams layoutParams = slider.getLayoutParams();
            layoutParams.width = width / 2;
            slider.setLayoutParams(layoutParams);

            sliderAnimate(index == 1, false);
        }
    }

    public void setSelectIndex(int index, boolean animate) {
        if (this.index == index) {
            return;
        }

        this.index = index;
        sliderAnimate(index == 1, animate);
    }

    public void setOnHandSwitchViewListner(OnHandSwitchViewListner onHandSwitchViewListner) {
        this.onHandSwitchViewListner = onHandSwitchViewListner;
    }

    public int setSelectIndex() {
        return index;
    }

    private void sliderAnimate(boolean right, boolean animate) {
        float fromValue;
        float toValue;
        if (right) {
            fromValue = 0;
            toValue = offset;
//            leftTv.setTypeface(Typeface.DEFAULT);
//            rightTv.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            fromValue = offset;
            toValue = 0;
//            leftTv.setTypeface(Typeface.DEFAULT_BOLD);
//            rightTv.setTypeface(Typeface.DEFAULT);
        }
        long duration = 1;
        if (animate) {
            duration = 250;
        }
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", fromValue ,toValue);
        ObjectAnimator.ofPropertyValuesHolder(slider, translationX).setDuration(duration).start();
    }

    private int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface OnHandSwitchViewListner {
        void onHandSwitchViewChanged(int index);
    }
}
